package com.huanwei.mylab;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.huanwei.cc.R;
import com.huanwei.cc.base.BaseActivity;
import com.huanwei.cc.bean.TypeBean;
import com.huanwei.cc.bean.Cons;
import com.huanwei.cc.bean.MEBQ;
import com.huanwei.mylab.adapter.MYDP_Adapater;
import com.huanwei.cc.bean.MeEvent;
import com.huanwei.mylab.adapter.YYJY_Adapter;
import com.huanwei.mylab.adapter.YYZL_Adapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ADM on 2017/12/4.
 * 我的标签
 */

public class Me_Label_Activity extends BaseActivity {
    private static final String TAG = "Me_Label_Activity";
    private TextView me_butten1;
    private TextView me_butten2;
    private GridView wd;
    private GridView yyjy;
    private GridView yyzl;
    private YYJY_Adapter yyjy_adapter;
    private YYZL_Adapter yyzl_adapter;
    private MYDP_Adapater mYbq_adapter;
    List<MEBQ> my_bqs = new ArrayList<>();
    List<String> me = new ArrayList<>();
    //存在是0 不存在是1
    int cunzai = 0;
    int position = 0;
    private TypeBean bbb1;
    private TypeBean bbb2;
    List<TypeBean.ResultdataBean> resultdata1;
    List<TypeBean.ResultdataBean> resultdata2;

    @Override
    public int setLayoutId() {

        return R.layout.me_lable;


    }

//    private void getvalue() {
//        API.StageType("0", new Okhttp.Objectcallback() {
//            @Override
//            public void onsuccess(String st) {
//                Log.e(TAG, "xx1" + st);
//                bbb1 = JSONUtils.parseJSON(st, TypeBean.class);
//                yyjy_adapter.clear();
//                resultdata1 = bbb1.getResultdata();
//                yyjy_adapter.addItem(resultdata1);
//            }
//
//            @Override
//            public void onFalia(int code, String errst) {
//                Log.e(TAG, "xx1x" + errst);
//                Toast.makeText(Me_Label_Activity.this, "网络异常", Toast.LENGTH_SHORT).show();
//            }
//        });
//        API.StageType("1", new Okhttp.Objectcallback() {
//            @Override
//            public void onsuccess(String st) {
//                Log.e(TAG, "xx2" + st);
//                bbb2 = JSONUtils.parseJSON(st, TypeBean.class);
//                yyzl_adapter.clear();
//                resultdata2 = bbb2.getResultdata();
//                yyzl_adapter.addItem(resultdata2);
//            }
//
//            @Override
//            public void onFalia(int code, String errst) {
//                Log.e(TAG, "xx2x" + errst);
//                Toast.makeText(Me_Label_Activity.this, "网络异常", Toast.LENGTH_SHORT).show();
//            }
//        });
//
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void setview() {
        me_butten2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Cons.xg = true;
                if (Cons.xg) {
                    mYbq_adapter.notifyDataSetChanged();
                }
            }
        });
        me_butten1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Cons.xg = false;
                mYbq_adapter.notifyDataSetChanged();
                for (int i = 0; i < my_bqs.size(); i++) {
                    me.add(my_bqs.get(i).getFishCategory_Name());
                }
            }
        });
        mYbq_adapter = new MYDP_Adapater();
        yyjy_adapter = new YYJY_Adapter();
        yyzl_adapter = new YYZL_Adapter();
        wd.setAdapter(mYbq_adapter);
        yyjy.setAdapter(yyjy_adapter);
        yyzl.setAdapter(yyzl_adapter);
        //养鱼经验
        yyjy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                TypeBean.ResultdataBean resultdata1 = (TypeBean.ResultdataBean) yyjy_adapter.getItem(position);
                Log.e(TAG, "xx1" + resultdata1.getFishCategory_Name());

                for (int i = 0; i < my_bqs.size(); i++) {
                    Log.e(TAG, "n1" + my_bqs.get(i).getFishCategory_Name());
                    if (my_bqs.get(i).getFishCategory_Name().equals(resultdata1.getFishCategory_Name())) {
                        cunzai = 1;
                        Log.e(TAG, "走1");
                    }

                }

                Log.e(TAG, "走4" + cunzai);
                if (cunzai == 1) {
                    Log.e(TAG, "走2");
                    cunzai = 0;
                } else {
                    mYbq_adapter.addItem(new MEBQ(resultdata1.getFishCategory_ID(), resultdata1.getFishCategory_Name()));
                    mYbq_adapter.notifyDataSetChanged();
                    cunzai = 0;
                    Log.e(TAG, "走3");
                    my_bqs.add(new MEBQ(resultdata1.getFishCategory_ID(), resultdata1.getFishCategory_Name()));
                }


            }
        });
        //养鱼种类
        yyzl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                TypeBean.ResultdataBean resultdata2 = (TypeBean.ResultdataBean) yyzl_adapter.getItem(position);
                Log.e(TAG, "xx2" + resultdata2.getFishCategory_Name());
                for (int i = 0; i < my_bqs.size(); i++) {
                    Log.e(TAG, "n1" + my_bqs.get(i).getFishCategory_Name());
                    if (my_bqs.get(i).getFishCategory_Name().equals(resultdata2.getFishCategory_Name())) {
                        cunzai = 1;
                        Log.e(TAG, "走1");
                    }

                }

                if (cunzai == 1) {
                    Log.e(TAG, "走2");
                    cunzai = 0;
                } else {
                    mYbq_adapter.addItem(new MEBQ(resultdata2.getFishCategory_ID(), resultdata2.getFishCategory_Name()));
                    mYbq_adapter.notifyDataSetChanged();
                    cunzai = 0;
                    Log.e(TAG, "走3");
                    my_bqs.add(new MEBQ(resultdata2.getFishCategory_ID(), resultdata2.getFishCategory_Name()));
                }

            }
        });
    }

    @Subscribe
    public void onEventMainThread(MeEvent event) {
        if (!TextUtils.isEmpty(event.getType())) {
        }
        if (event.getType().equals("remove")) {
            String content = event.getContent();
            for (int i = 0; i < my_bqs.size(); i++) {
                if (my_bqs.get(i).getFishCategory_Name().equals(content)) {
                    position = i;
                }

            }
            my_bqs.remove(position);
        }
    }

    @Override
    public void initViewsAndEvents() {
        EventBus.getDefault().register(this);
        me_butten1 = (TextView) findViewById(R.id.me_butten1);
        me_butten2 = (TextView) findViewById(R.id.me_butten2);
        wd = (GridView) findViewById(R.id.wd);
        yyjy = (GridView) findViewById(R.id.yyjy);
        yyzl = (GridView) findViewById(R.id.yyzl);
        setview();


    }
}
